package com.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.core.exception.ParameterException;
import com.demo.dao.*;
import com.demo.domain.MessageView;
import com.demo.service.MessageService;
import com.demo.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageDao,MessageView> implements MessageService {

    @Autowired
    private RegistrationInformationDao registrationInformationDao;

    @Autowired
    private ReplyDao replyDao;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private PostDao postDao;

    @Override
    public boolean hasNotReadMessage(Long uid) {
        try {
            if (uid == null || uid <= 0L) {
                throw new ParameterException("uid不正确");
            }
            //查询未读信息个数
            int replyCount = replyDao.countNotViewReply(uid);
            List<Long> pids = postDao.selectPidByuid(uid);
            int commentCount = 0;
            for (Long pid : pids) {
                commentCount += commentDao.countNotViewComment(pid);
            }
            boolean hasNotRead = registrationInformationDao.countNotViewRegistration(uid) + replyCount + commentCount > 0;
            return hasNotRead;
        } catch (ParameterException e) {
            // 在这里处理异常，你可以选择抛出新的异常、记录日志或者返回默认值等
            e.printStackTrace(); // 可以选择记录日志
            return false; // 返回默认值
        }
    }

}
